<route lang="json5">
{
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '会议室预约',
    enablePullDownRefresh: true,
  },
}
</route>
<template>
  <th-page-box>
    <template #tabBar>
      <view class="tab_bar_inner">
        <wd-navbar title="会议室预约" :bordered="false" safeAreaInsetTop>
          <template #left>
            <wd-icon name="thin-arrow-left" size="22px" color="white" @click="goBack"></wd-icon>
          </template>
        </wd-navbar>
        <view class="cu-bar search-container">
          <view class="search-form round">
            <wd-input
              type="text"
              prop="code"
              v-model="state.queryForm.code"
              no-border
              clearable
              placeholder="请输入会议代号"
              style="
                display: flex;
                align-items: center;
                width: 100%;
                height: 100%;
                padding: 0 20rpx;
              "
              @confirm="handleSearch"
              confirm-type="search"
            />
            <text class="filter-icon cuIcon-searchlist" @click="filterIsShow = true"></text>
          </view>
          <view class="action">
            <button
              @click="handleSearch()"
              class="cu-btn bg-gradual-green shadow-blur round"
              style="margin-right: 10rpx"
            >
              查询
            </button>
            <button
              @click="handleAdd()"
              class="cu-btn bg-gradual-blue shadow-blur round"
              v-if="checkPremiss('work_conference_room_make_an_appointment_add')"
            >
              预约
            </button>
          </view>
        </view>
        <view class="tabs">
          <view
            v-for="(item, index) in tableList"
            :class="['item', 'i' + index, item.value === activeTab ? 'on' : '']"
            :key="item.value"
            @click="onTabChange(item.value)"
          >
            {{ item.label }}
          </view>
        </view>
      </view>
    </template>
    <template #inner>
      <view class="content" style="padding-top: 100px; background-color: #f8f8f8">
        <template v-for="item in dataList" :key="item.id">
          <card
            :_D="item"
            page="/pages/woek/makeAnAppointment/flow-detail"
            :isOpt="true"
            @refresh="refresh"
          ></card>
        </template>
        <wd-loadmore
          :state="loadMore"
          finishedText="没有更多了"
          @reload="handleSearch()"
        ></wd-loadmore>
        <!-- <view class="inner">
          <thCollapses
            v-model:list="state.staticMenu"
            :objInfo="{
              value: 'id',
              label: 'name',
              children: 'children',
            }"
            :isShowLeftIcon="isShowLeftIcon"
            :isAccordion="true"
            @click="toDetail"
          ></thCollapses>
          <wd-loadmore
            v-if="!state.staticMenu || !state.staticMenu.length"
            state="finished"
            finishedText="暂时无数据"
          ></wd-loadmore>
        </view> -->
      </view>
      <wd-popup
        v-model="filterIsShow"
        position="bottom"
        :close-on-click-modal="false"
        custom-style="padding: 32rpx;border-top-left-radius:60rpx;border-top-right-radius:60rpx;"
        @close="handleClose"
      >
        <view class="cu-dialog" @click.stop="">
          <view class="cu-bar bg-white" style="min-height: 60rpx !important">
            <view class="action"></view>
            <view class="action mks" @click="filterIsShow = false">取消</view>
          </view>
          <view class="filter_container">
            <view class="block">
              <view class="label">会议代号</view>
              <view class="row">
                <input type="text" v-model="state.queryForm.code" placeholder="请输入会议代号" />
              </view>
            </view>
            <!-- <view class="block">
              <view class="label">主题</view>
              <view class="row">
                <input type="text" v-model="state.queryForm.topic" placeholder="请输入主题" />
              </view>
            </view> -->
            <!-- <view class="block">
              <view class="label">会议室</view>
              <view class="row">
                <wd-picker
                  :columns="state.queryForm.roomOptions"
                  v-model="state.queryForm.roomId"
                  placeholder="请选择会议室"
                />
              </view>
            </view> -->

            <view class="block">
              <view class="label">开始日期</view>
              <view class="row">
                <uni-datetime-picker
                  type="date"
                  return-type="string"
                  v-model="state.queryForm.startTime"
                  placeholder="开始日期"
                />
              </view>
            </view>
            <view class="block">
              <view class="label">结束日期</view>
              <view class="row">
                <uni-datetime-picker
                  type="date"
                  return-type="string"
                  v-model="state.queryForm.endTime"
                  placeholder="结束日期"
                />
              </view>
            </view>
            <view class="block" style="margin-top: 60rpx">
              <view class="row">
                <view class="btn" @click="resetFilter">重置</view>
                <view class="btn primary" @click="handleSearch()">查询</view>
              </view>
            </view>
          </view>
        </view>
      </wd-popup>
    </template>
  </th-page-box>
</template>

<script lang="ts" setup name="workConferenceRoomMakeAnAppointment">
import { useSetting } from '@/stores/setting';
import { fetchList } from '@/service/work/conferenceRoom/makeAnAppointment';
import { formatByDayjs } from '@/utils/util';
import { useDict } from '@/hooks/dict';
import { useUserStore } from '@/stores/user';
import card from './component/card.vue';

const dataList = ref([]);
const setting = useSetting();

const page = ref(1);
const loading = ref(false);
// const { bill_status } = useDict('bill_status');
const loadMore = ref<any>('finished');
const userStore: any = useUserStore();
const filterIsShow = ref(false);

const tableList = ref([
  {
    label: '全部',
    value: null,
    color: '#108ee9',
  },
  // {
  //   label: '待办',
  //   value: '-1',
  //   color: '#108ee9',
  // },
  {
    label: '待提交',
    value: '0',
    color: '#108ee9',
  },
  {
    label: '审核中',
    value: '1',
    color: '#d46b08',
  },
  {
    label: '通过',
    value: '2',
    color: '#87d068',
  },
  {
    label: '拒绝',
    value: '3',
    color: '#f50',
  },
  // {
  //   text: '撤回',
  //   value: '9',
  //   color: '#f50',
  // },
]);

const state = reactive({
  queryForm: {
    status: null,
    size: 20,
    code: null,
    topic: null,
    roomId: null,
    endTime: null,
    startTime: null,
    date: '',
  },
});

// onLoad((options) => {
// tableList.value = [
//   {
//     label: '全部',
//     value: null,
//     color: '#108ee9',
//   },
//   ...bill_status.value,
// ];
// getData();
// });

onShow(() => {
  refresh();
});

const resizeParams = (data) => {
  const o = {};
  Object.keys(data).forEach((item) => {
    if (data[item] !== null && data[item] !== '') {
      o[item] = data[item];
    }
  });
  return o;
};

const getData = (isShowLoading = true) => {
  if (isShowLoading) {
    uni.showLoading();
    loading.value = true;
  }
  const params = resizeParams(state.queryForm);
  loadMore.value = 'loading';
  fetchList({
    current: page.value,
    descs: 'create_time',
    ...params,
  })
    .then((res) => {
      if (res.code !== 0) {
        uni.showToast({
          title: res.msg,
          icon: 'none',
        });
        return;
      }
      dataList.value = dataList.value.concat(res.data.records);
      if (res.data.records.length === state.queryForm.size) {
        loadMore.value = 'loadEnd';
      } else {
        loadMore.value = 'finished';
      }
    })
    .catch((err) => {
      loadMore.value = 'finished';
      uni.showToast({
        title: err.data?.msg || err.msg,
        icon: 'none',
      });
    })
    .finally(() => {
      if (isShowLoading) {
        uni.hideLoading();
        loading.value = false;
      }
      uni.stopPullDownRefresh();
    });
};

const goBack = () => {
  uni.navigateBack();
};

// watch(
//   () => bill_status.value,
//   (n) => {
//     console.log('=----b', n);
//     tableList.value = [
//       {
//         label: '全部',
//         value: null,
//         color: '#108ee9',
//       },
//       ...n,
//     ];
//   },
//   {
//     deep: true,
//     immediate: true,
//   },
// );

const handleSearch = (e?) => {
  // if (e !== undefined) {
  //   state.demandCode = e;
  // }
  // console.log(demandDateList.value);
  // let startDate = demandDateList.value[0];
  // let endDate = demandDateList.value[1];

  // 判断时间
  const startDate = state.queryForm.startTime;
  const endDate = state.queryForm.endTime;
  if (endDate && new Date(startDate).getTime() > new Date(endDate).getTime()) {
    uni.showToast({
      title: '开始时间不能大于结束时间',
      icon: 'none',
      mask: true,
    });
    return;
  }
  // if (endDate || startDate) {
  //   startDate = startDate
  //     ? formatByDayjs(startDate, 'YYYY-MM-DD 00:00:00')
  //     : formatByDayjs(endDate, 'YYYY-MM-DD 00:00:00');
  //   endDate = endDate
  //     ? formatByDayjs(endDate, 'YYYY-MM-DD 00:00:00')
  //     : formatByDayjs(startDate, 'YYYY-MM-DD 00:00:00');
  //   state.queryForm.webSql = `demand_date between '${startDate}' and '${endDate}'`;
  // } else {
  //   state.queryForm.webSql = null;
  // }
  dataList.value = [];
  page.value = 1;
  // state.demandDate = state.demandDate
  //   ? formatByDayjs(state.demandDate, 'YYYY-MM-DD 00:00:00')
  //   : null;
  getData();
  filterIsShow.value = false;
};
const handleAdd = () => {
  uni.navigateTo({
    url: `/pages/work/conferenceRoom/makeAnAppointment/form`,
  });
};

const activeTab = ref(null);

const onTabChange = (e) => {
  activeTab.value = e;
  state.queryForm.status = e;
  dataList.value = [];
  page.value = 1;
  handleSearch();
  // resetFilter();
};

onPullDownRefresh(() => {
  // 重新获取
  // if (loading.value) return;
  // dataList.value = [];
  // page.value = 1;
  // getData();
  refresh();
});

onReachBottom(() => {
  if (loading.value) return;
  // page.value++;
  // getData();
  if (loadMore.value === 'loadEnd') {
    page.value++;
    getData();
  }
});

const refresh = (e?) => {
  dataList.value = [];
  page.value = 1;
  getData(e);
};

uni.$on('refresh', () => {
  refresh(false);
});

const handleClose = () => {
  filterIsShow.value = false;
};

const resetFilter = () => {
  handleClose();

  state.queryForm.topic = null;
  state.queryForm.roomId = null;
  demandDateList.value = ['', ''];
  state.queryForm.startTime = null;
  state.queryForm.endTime = null;
  handleSearch();
};

const demandDateList = ref(['', '']);
const displayFormatTabLabel = (items) => {
  return `${items[0].label}年${items[1].label}月${items[2].label}日`;
};

const handleConfirm = ({ value }) => {
  console.log(new Date(value));
};

const checkPremiss = (k) => {
  return userStore.userInfos.authBtnList.some((v: string) => v === k);
};
</script>

<style lang="scss">
$padding: 30rpx;

.tab_bar_inner {
  background-image: linear-gradient(45deg, #0081ff, #1ab3f5);
  :deep(.wd-navbar) {
    background-color: rgba(0, 0, 0, 0) !important;
    background-image: linear-gradient(45deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0)) !important;
  }
  // .cu-bar.search-container {
  //   background-image: linear-gradient(45deg, #0081ff, #1ab3f5);
  // }
  :deep(uni-button:after) {
    border: 1px solid rgba(0, 0, 0, 0);
  }
}

.canvas-box {
  .canvas {
    height: 300rpx;
    transition: height 0.3s;
  }

  .btns {
    margin-top: 10rpx;
    transition: transform 0.3s;

    .btn {
      width: auto;
      height: 50rpx;
    }
  }
}

.full-screen {
  flex-direction: row;
  height: calc(100vh - 88rpx);

  .canvas {
    width: calc(100% - 100rpx);
    height: 100%;
    margin-left: 100rpx;
  }

  .btns {
    position: absolute;
    align-items: center;
    width: calc(100vh - (88rpx + $padding * 2));
    height: 100rpx;
    transform: translate(100rpx, 0) rotate(90deg);
    transform-origin: top left;

    .btn-box {
      flex-direction: row;
    }
  }
}

.tabbar {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  background: linear-gradient(45deg, #0081ff, #1ab3f5);
}

.tabbar_c {
  height: 100%;
  font-weight: 800;
  color: white;
  letter-spacing: 2px;
  background: linear-gradient(45deg, #0081ff, #1ab3f5);
}

.bolck {
  background: linear-gradient(45deg, #0081ff, #1ab3f5);
}

.task_list {
  background: #f7f7f7;
}

.cu-bar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 50px;
}

.cu-bar .search-form {
  display: flex;
  flex: 1;
  align-items: center;
  height: 64rpx;
  margin: 0 30rpx;
  overflow: hidden;
  /* padding: 0 40rpx; */
  font-size: 24rpx;
  line-height: 64rpx;
  color: #333;
  background-color: white;
}

.search-container .filter-icon {
  padding: 0 18rpx;
  margin: 0;
  font-size: 40rpx;
  color: #007aff;
  border-left: 1rpx solid #f3f3f3;
}

.round {
  border-radius: 2500px;
}

.cu-bar .search-form + .action {
  margin-right: 30rpx;
}

.cu-bar .action {
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  height: 100%;
}

.search-container .action .cu-btn {
  padding: 0 24rpx;
}

.bg-gradual-green {
  color: #fff;
  background-image: linear-gradient(45deg, #39b54a, #8dc63f);
}

.bg-gradual-blue {
  color: #fff;
  background-image: linear-gradient(45deg, #0c62d3, #2a73d3);
}

.cu-btn {
  position: relative;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 64rpx;
  padding: 0 30rpx;
  margin-right: initial;
  margin-left: initial;
  overflow: visible;
  font-size: 28rpx;
  line-height: 1;
  text-align: center;
  text-decoration: none;
  border: 0;
  transform: translate(0, 0);
}

.tabs {
  display: flex;
  justify-content: space-between;
  padding: 0 50rpx;
  margin-top: 15rpx;

  .item {
    position: relative;
    padding: 15rpx 0;
    font-size: 30rpx;
    color: #fff;
    text-align: center;
    opacity: 0.5;
    transition: all 0.3s;

    &.on {
      opacity: 1;

      &::after {
        position: absolute;
        bottom: -8rpx;
        left: 50%;
        width: 12rpx;
        height: 12rpx;
        content: '';
        background: #f1f1f1;
        border-radius: 50%;
        transform: translate(-50%);
      }
    }
  }
}

// :deep(.wd-navbar) {
//   background: inherit;
// }

.cu-dialog {
  font-size: 28rpx;
}

.cu-dialog > .cu-bar:first-child .action {
  margin-right: 20rpx;
}

.filter_container {
  padding: 10rpx 32rpx 32rpx;
  padding-bottom: 64rpx;
  text-align: left;
  background-color: #fff;

  .block {
    ~ .block {
      margin-top: 32rpx;
    }

    .label {
      margin-bottom: 20rpx;
      font-weight: bold;
    }
  }

  .row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;

    .date-picker {
      flex: 1;
      padding: 16rpx;
      text-align: center;
      background-color: #f7f7f7;
      border-radius: 8rpx;
    }

    .select-box {
      flex: 1;
      padding: 16rpx;
      background-color: #f7f7f7;
      border-radius: 8rpx;

      :deep(.select-row) {
        padding: 0;
        background: none;
      }
    }

    input {
      flex: 1;
      height: 33rpx;
      padding: 16rpx 12rpx;
      font-size: 26rpx !important;
      background-color: #f7f7f7;
      border: 1rpx solid #f7f7f7;
      border-radius: 8rpx;

      .input-placeholder {
        color: #8a929f;
      }
    }

    .btn {
      flex: 1;
      padding: 16rpx;
      color: #999;
      text-align: center;
      background-color: #f7f7f7;
      border: 1rpx solid #dedede;
      border-radius: 8rpx;

      ~ .btn {
        margin-left: 24rpx;
      }

      &.primary {
        color: #fff;
        background-color: #007aff;
        border-color: #007aff;
      }
    }
  }
}

:deep(.wd-navbar__left--hover) {
  background: none;
}

.s-date {
  .wd-picker__cell {
    padding: 0 !important;
  }
}

.form-item {
  padding: 24rpx 0 !important;
  margin: 0 !important;
}
</style>
